package com.qdlc.p2p.dal.model;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;

import com.qdlc.p2p.common.exception.UpRateException;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.common.util.ValidateUtil;
import com.qdlc.p2p.dal.dto.RateRule;

/**
 * 加息券规则model
 * @author wjx
 * @version 2.0
 * @date 2015年4月22日 下午3:53:39
 */
public class RateRuleModel extends RateRule {
	
	private static  Logger logger = Logger.getLogger(RateRuleModel.class);
	
	/** 状态-未启用 */
	public static final byte RATERULE_STATUS_DISABLE = 2;
	/** 状态-启用 */
	public static final byte  RATERULE_STATUS_ENABLE = 1;
	
	/**
	 * 查询名称
	 */
	private String searchName;

	/** 当前页码 */
	private int page = 1;

	/** 每页数据条数 */
	private int rows = 5;
	
	/**
	 * 过期开始时间
	 */
	private String expiredStartTime;
	/**
	 * 过期结束时间
	 */
	private String expiredEndTime;
	
	
	public static RateRuleModel instance(RateRule rateRule) {
		RateRuleModel reteRuleModel = new RateRuleModel();
		BeanUtils.copyProperties(rateRule, reteRuleModel);
		return reteRuleModel;
	}
	
	/**
	 * 将当前model的属性封装到实体bean中
	 * @return
	 */
	public RateRule prototype(){
		RateRule rateRule = new RateRule();
		BeanUtils.copyProperties(this, rateRule);
		return rateRule;
		
	}
	
	/**
	 * 初始化加息券规则时，计算结束时间以及基本参数设定
	 * @return
	 */
	public RateRule prototypeForAppend(){
		RateRule rateRule = this.prototype();
		rateRule.setIsUse(RateRuleModel.RATERULE_STATUS_ENABLE);
		return rateRule;
	}
	
	
	public void validModelData(){
		if (!ValidateUtil.isInteger(getPayTotal())) { //校验发放总数是不是整数
			throw new UpRateException(MessageUtil.getMessage("MF002002013"));
		}
		if (StringUtil.isBlank(getRuleName())) {//检验名称是否为空
			throw new UpRateException(MessageUtil.getMessage("MF002002010"));
		}
		if (getPayWay() ==1 && getUpApr() < 0.01) {//检验加息比例是否合规
			throw new UpRateException(MessageUtil.getMessage("MF002002011"));
		}
		if (getValitTime() < 1) {
			throw new UpRateException(MessageUtil.getMessage("MF002002012"));
		}
		if (getPayTotal() < 1) {
			throw new UpRateException(MessageUtil.getMessage("MF002002013"));
		}
	}
	
	
	/**
	 * 校验加息券规则的有效性
	 */
	public boolean checkRateRule(Date date,int realPayTotal){
		//校验规则是否启用
		if (getIsUse() ==UpRateFinalType.RATE_RULE_STATUS_FAIL) {
			logger.info(MessageUtil.getMessage("MF002002005"));
			return false;
		}
		
		
		//判断已经发放数量是否达到活动设置的上限值
		if (getPayTotal()>0 && realPayTotal>=getPayTotal()) {
			logger.info(MessageUtil.getMessage("MF002002006"));
			return false;
		}
		return true;
	
	}

	public String getSearchName() {
		return searchName;
	}

	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getExpiredStartTime() {
		return expiredStartTime;
	}

	public void setExpiredStartTime(String expiredStartTime) {
		this.expiredStartTime = expiredStartTime;
	}

	public String getExpiredEndTime() {
		return expiredEndTime;
	}

	public void setExpiredEndTime(String expiredEndTime) {
		this.expiredEndTime = expiredEndTime;
	}


	
	
}
